import md5 from "js-md5";
import {decrypt} from "@/utils";
export const checkHeart = (done) => {
  let isProd = process.env.VUE_APP_PROD === 'false' ? false : true;
  if(isProd){
    const urls = [
      "https://bs.glvroc.com",
      "https://bs.shun8899.com",
      "https://bs.shun9988.com"
    ];
    let prodUrl = '';
    urls.forEach( url => {
      const img = new Image();
      img.src = url+'/svg/loading-spin.svg?_=' + new Date().getTime();
      img.onload = () => {
        if(!prodUrl){
          // 这里的config包含每次请求的内容
          let rtyu = "KiqdJOMcbnPL2NlixR6q7yQopjtMeBKyzSOwryI5IzM=";
          let hjkl = process.env.VUE_APP_KEY;
          let qwer = "0";
          let asdf = parseInt(Math.random() * 1000000);
          let zxcv = parseInt(new Date().getTime() / 1000);
          let eirn = decrypt(rtyu, hjkl);
          let fghj = "/admin/mainTian/getByTenantId";
          fghj = fghj.toLocaleLowerCase();
          let iop = md5(fghj + "-" + zxcv + "-" + asdf + "-" + qwer + "-" + eirn);
          let sign = zxcv + "-" + asdf + "-" + qwer + "-" + iop;
          prodUrl = url;
          let method = 'GET';
          let params = {
            tenantId: process.env.VUE_APP_TENANTID,
            sign
          };
          var pairs = [];
          for (var key in params) {
            pairs.push(key + "=" + params[key]);
          }

          var querystring = pairs.join("&");
          var xhr = window.XMLHttpRequest
            ? new XMLHttpRequest()
            : new ActiveXObject("Microsoft.XMLHTTP");
            xhr.addEventListener("readystatechange", function() {
              if (this.readyState !== 4) return;
              try {
                done(JSON.parse(xhr));
              } catch (e) {
                done(xhr);
              }
            });

          if (method === "GET") {
            prodUrl += "/admin/mainTian/getByTenantId?" + querystring;
          }

          xhr.open(method, prodUrl);
          var data = null;

          xhr.send(data);
        }
      };
    } );
  }else{
    // 这里的config包含每次请求的内容
    let rtyu = "KiqdJOMcbnPL2NlixR6q7yQopjtMeBKyzSOwryI5IzM=";
    let hjkl = process.env.VUE_APP_KEY;
    let qwer = "0";
    let asdf = parseInt(Math.random() * 1000000);
    let zxcv = parseInt(new Date().getTime() / 1000);
    let eirn = decrypt(rtyu, hjkl);
    let fghj = "/admin/mainTian/getByTenantId";
    fghj = fghj.toLocaleLowerCase();
    let iop = md5(fghj + "-" + zxcv + "-" + asdf + "-" + qwer + "-" + eirn);
    let sign = zxcv + "-" + asdf + "-" + qwer + "-" + iop;
    let method = 'GET';
    let testUrl = 'http://test-boss-embc1.163220.com/admin/mainTian/getByTenantId';
    let url = testUrl;
    let params = {
      tenantId: process.env.VUE_APP_TENANTID,
      sign
    };
    var pairs = [];
    for (var key in params) {
      pairs.push(key + "=" + params[key]);
    }

    var querystring = pairs.join("&");
    var xhr = window.XMLHttpRequest
      ? new XMLHttpRequest()
      : new ActiveXObject("Microsoft.XMLHTTP");
      xhr.addEventListener("readystatechange", function() {
        if (this.readyState !== 4) return;
        try {
          done(JSON.parse(xhr));
        } catch (e) {
          done(xhr);
        }
      });

    if (method === "GET") {
      url += "?" + querystring;
    }

    xhr.open(method, url);
    var data = null;

    xhr.send(data);
  }
};
